/*
 * @Description: 转成嵌套的tokens
 * @Author: wangqi
 * @Date: 2021-05-06 21:02:00
 * @LastEditTime: 2021-05-06 21:47:34
 */

function nestTokens(tokens) {
    // 最终形成的嵌套数组
    let nestedTokens = [];
    // 栈结构
    let sections = [];
    // 用于收集嵌套数据
    let collector = nestedTokens;

    for (let i = 0; i < tokens.length; i++) {
        let token = tokens[i];
        switch (token[0]) {
            case "#":
                collector.push(token);
                sections.push(token);
                collector = token[2] = [];
                break
            case "/":
                sections.pop();
                // 回到上一层的栈数据, 这样以后的数据就push 到上一层的 # 对象中
                collector = sections.length > 0 ? sections[sections.length - 1][2] : nestedTokens;
                break
            default:
                collector.push(token);
        }
    };
    return nestedTokens;
};

export default nestTokens;
